package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.MyMedalEntity;
import com.runen.infinitemovement.tool.AnimationUtils;
import com.runen.infinitemovement.widget.popupwind.MyMedalPoppupWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by LiaoDuanHong
 * Create on 2020/12/30  18:02
 */
public class MyMedalAdapter extends BaseQuickAdapter<MyMedalEntity.DataBean, BaseViewHolder> {
    private Context context;
    private int type;

    public OnClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    private OnClickListener onClickListener;

    public MyMedalAdapter(Context context, int type) {
        super(R.layout.item_my_medal);
        this.context = context;
        this.type = type;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, MyMedalEntity.DataBean item) {
        List<MyMedalEntity.DataBean.MedalsBean> medalsBeans = new ArrayList<>();
        TextView tvPathName = helper.getView(R.id.tv_path_name);
        ImageView ivUpDown = helper.getView(R.id.iv_up_down);
        ImageView ivRedLongLabel = helper.getView(R.id.iv_red_long_label);
        RecyclerView rvItemList = helper.getView(R.id.rv_item_list);
        if (!item.isCheck) {
            AnimationUtils.setRotateAnimation(ivUpDown, 0, 180);
        } else {
            AnimationUtils.setRotateAnimation(ivUpDown, 180, 0);
        }
        LinearLayoutManager layoutManager = new GridLayoutManager(context, 3);
        rvItemList.setLayoutManager(layoutManager);
        MyMedalItemAdapter itemAdapter = new MyMedalItemAdapter(context);
        rvItemList.setAdapter(itemAdapter);
        if (item.gotCount == null || item.totalCount == null) {
            String name = "%s";
            tvPathName.setText(String.format(name, item.name));
        } else {
            String name = "%s（%s/%s）";
            tvPathName.setText(String.format(name, item.name, item.gotCount, item.totalCount));
        }
        switch (type) {
            case 1:
                ivRedLongLabel.setImageResource(R.mipmap.ico_red_long_label1);
                break;
            case 2:
                switch (item.type) {
                    case "mileage":
                        ivRedLongLabel.setImageResource(R.mipmap.ico_red_long_mile_label);
                        break;
                    case "running":
                        ivRedLongLabel.setImageResource(R.mipmap.ico_red_long_run_label);
                        break;
                    case "walking":
                        ivRedLongLabel.setImageResource(R.mipmap.ico_red_long_walk_label);
                        break;
                    case "riding":
                        ivRedLongLabel.setImageResource(R.mipmap.ico_red_long_riding_label);
                        break;
                }
                break;
            case 3:
                ivRedLongLabel.setImageResource(R.mipmap.ico_red_long_company_label2);
                break;
        }

        if (item.medals != null) {
            for (int i = 0; i < item.medals.size(); i++) {
                if (!item.isCheck) {
                    if (i < 3) {
                        medalsBeans.add(item.medals.get(i));
                    }
                } else {
                    medalsBeans.add(item.medals.get(i));
                }
            }
        }
        itemAdapter.setNewData(medalsBeans);
        ivUpDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!item.isCheck) {
                    item.isCheck = true;
                    AnimationUtils.setRotateAnimation(ivUpDown, 0, 180);
                } else {
                    item.isCheck = false;
                    AnimationUtils.setRotateAnimation(ivUpDown, 180, 0);
                }
                if (null != onClickListener) {
                    onClickListener.ocClick(item, helper.getAdapterPosition());
                }
            }
        });

        itemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (itemAdapter.getData().get(position).got) {
                    MyMedalPoppupWindow poppupWindow = MyMedalPoppupWindow.getInstance(context, itemAdapter.getData().get(position));
                    poppupWindow.show();
                }
            }
        });
    }

    public interface OnClickListener {
        void ocClick(MyMedalEntity.DataBean item, int position);
    }
}
